<?php
/**
 * Created by PhpStorm.
 * User: zouyanan
 * Date: 2019/5/11
 * Time: 下午10:35
 */

namespace app\userapi\controller;
use app\common\controller\Api;


/**
 *  经济人主页接口
 */
class Centeragent extends Api
{
    protected $noNeedLogin = ['index','houseList'];
    protected $noNeedRight = '*';


    public function  index(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $agent_id = $this->request->request("agent_id");
        $info = db("user a")
            //->join("user_agent b","a.id = b.user_id","LEFT")
            ->field("a.id,a.mobile,a.nickname,a.avatar")
            ->field("a.image,a.fans")
            ->where("a.id",$agent_id)
            ->find();
        $attention_id = db("yjy_attention")->where(['agent_id'=>$agent_id,'user_id'=>$userInfo['id']])->value("id");
        if($attention_id){  //是否关注
            $info['is_attention'] = 1;
        }else{
            $info['is_attention'] = 0;
        }
        $this->success("返回成功",$info);
    }



     /**
      * 关注/取消关注
      */
     public function attention()
     {
         header('Access-Control-Allow-Origin: *');
         header('Access-Control-Allow-Credentials: true');
         header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
         header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
         $userInfo = $this->auth->getUserinfo();
         $agent_id = $this->request->request("agent_id");
         $action = $this->request->request("action");//add=添加关注,cancel=取消关注
         if (!$agent_id || !$action) {
             $this->error("缺少参数");
         }
         if ($action == 'add') {
             $res_a = db("yjy_attention")->insert([
                 'user_id' => $userInfo['id'],
                 'agent_id' => $agent_id,
                 'createtime' => time()
             ]);
             $res_b = db('user')->where('id', $agent_id)->setInc("fans", 1);
             $res_c = db("user")->where("id", $userInfo['id'])->setInc("num_attention", 1);
             $msg = '关注';
         }elseif($action == 'cancel'){
             db("yjy_attention")->where('user_id',$userInfo['id'])->where('agent_id',$agent_id)->delete();
             db('user')->where('id', $agent_id)->setDec("fans", 1);

             $res_c = db("user")->where("id", $userInfo['id'])->setDec("num_attention", 1);
             $msg = '取消关注';
         }
         if($res_c){
             $this->success($msg."成功");
         }else{
             $this->error($msg."失败");
         }
     }










    //经纪人主页房源列表
    public function houseList(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $agent_id = $this->request->request("agent_id");
        $area_id = $this->request->request("area_id");
        $mid = $this->request->request("mid");
        //区域
        $area = db("yjy_house a")
            ->join("yjy_house_area b","a.area_id=b.id","LEFT")
            ->where("a.user_id",$agent_id)
            ->field("a.area_id,b.name")
            ->group("a.area_id")
            ->select();
        $where['user_id'] = $agent_id;
        if($mid ==1){

            $where['area_id'] = $area[0]['area_id'];
        }else{

            $where['area_id'] = $area_id;
        }




        //if($area_id){

       // }
        $house = db("yjy_house")
            ->field("Id,title,price,location_near,image,images")
            ->where($where)
            ->select();
        $data = [
            'area'    =>   $area,
            'house'   =>   $house
        ];

        $this->success("返回成功",$data);


    }































}